package com.service.impl;

import com.mapper.MediaMapper;
import com.pojo.Media;
import com.pojo.Result;
import com.service.MediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class MediaServiceImpl implements MediaService {

    @Autowired
    private MediaMapper mediaMapper;

    @Override
    public Result<Media> createMedia(Media media) {
        try {
            // 设置创建时间
            media.setCreatedAt(LocalDateTime.now());

            // 设置默认排序索引
            if (media.getOrderIndex() == null) {
                // 获取当前笔记的最大排序索引
                Integer maxOrder = mediaMapper.selectMaxOrderIndexByNoteId(media.getNoteId());
                media.setOrderIndex(maxOrder != null ? maxOrder + 1 : 1);
            }

            int result = mediaMapper.insert(media);
            if (result > 0) {
                return Result.success(media);
            } else {
                return Result.error("创建媒体资源失败");
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Void> updateMedia(Media media) {
        try {
            int result = mediaMapper.update(media);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("更新媒体资源失败，可能ID不存在");
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Void> deleteMedia(Integer id) {
        try {
            int result = mediaMapper.delete(id);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("删除媒体资源失败，可能ID不存在");
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Void> deleteMediaByNoteId(Integer noteId) {
        try {
            int result = mediaMapper.deleteByNoteId(noteId);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.success(); // 没有媒体资源也算成功
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Media> getMediaById(Integer id) {
        try {
            Media media = mediaMapper.selectById(id);
            if (media != null) {
                return Result.success(media);
            } else {
                return Result.error("媒体资源不存在");
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<List<Media>> getMediaByNoteId(Integer noteId) {
        try {
            List<Media> mediaList = mediaMapper.selectByNoteId(noteId);
            return Result.success(mediaList);
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<List<Media>> getMediaByType(String mediaType) {
        try {
            List<Media> mediaList = mediaMapper.selectByMediaType(mediaType);
            return Result.success(mediaList);
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Void> updateMediaOrder(Integer id, Integer orderIndex) {
        try {
            int result = mediaMapper.updateOrderIndex(id, orderIndex);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("更新排序索引失败，可能ID不存在");
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }
}